<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card class="box-card">
      <el-alert
        title="添加商品信息"
        type="info"
        :closable="false"
        center
        show-icon
      >
      </el-alert>
      <!-- 步骤条 -->
      <el-steps
        finish-status="success"
        align-center
        class="box-card"
        :active="active * 1"
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <!-- 表单 -->
      <el-form :model="addform">
        <!-- 标签 -->
        <el-tabs
          tab-position="left"
          class="box-card"
          v-model="active"
          :before-leave="leave"
          @tab-click="handleClick"
          :active="active"
        >
          <el-tab-pane label="基本信息">
            <el-form>
              <el-form-item label="商品名称" name="0">
                <el-input></el-input>
              </el-form-item>
              <el-form-item label="商品价格" name="1">
                <el-input value="0"></el-input>
              </el-form-item>
              <el-form-item label="商品重量" name="2">
                <el-input value="0"></el-input>
              </el-form-item>
              <el-form-item label="商品数量" name="3">
                <el-input value="0"></el-input>
              </el-form-item>
              <el-form-item label="商品分类" name="4">
                <el-cascader
                  v-model="checkyyy"
                  :options="options"
                  :props="{
                    expandTrigger: 'hover',
                    value: 'cat_id',
                    label: 'cat_name'
                  }"
                  @change="handleChange"
                ></el-cascader>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="商品参数">
            <!-- 渲染动态参数 -->
            <el-form>
              <el-form-item
                :label="item.attr_name"
                v-for="item in manydongtai"
                :key="item.id"
              >
                <el-checkbox-group v-model="item.attr_vals">
                  <el-checkbox
                    :label="item1"
                    v-for="(item1, index) in item.attr_vals"
                    :key="index"
                  ></el-checkbox>
                </el-checkbox-group>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="商品属性">
            <!-- 渲染静态属性 -->
            <el-form>
              <el-form-item
                :label="item.attr_name"
                v-for="item in onlyjingtai"
                :key="item.id"
              >
                <el-input v-model="item.attr_vals"></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="商品图片">
            <!-- 图片上传 -->
            <el-upload
              action="http://www.ysqorz.top:8888/api/private/v1/upload"
              :headers="headers"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-success="handlesapp"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容">商品内容</el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { dongtai, goodstype } from '../http/api'
export default {
  data() {
    return {
      // 设置请求头
      headers: {
        Authorization: sessionStorage.getItem('token')
      },
      active: 0,
      options: [],
      checkyyy: [], // 3级分类选中的数组
      // 动态参数
      manydongtai: '',
      // 静态参数
      onlyjingtai: '',
      // 要提交的商品表单的数据
      addform: {
        goods_name: '',
        goods_cat: '',
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0,
        goods_introduce: 0,
        pics: [],
        attrs: []
      }
    }
  },
  created() {
    this.auto()
  },
  methods: {
    // 触发预览效果
    handlePreview() {},
    // 上传成功
    handlesapp(res) {
      this.addform.push({ pic: res.data.tmp_path })
    },
    // 删除上传
    handleRemove(res) {
      const index = this.addform.pics.findIndex((item) => {
        return item.pic === res.response.data.tmp_path
      })
      this.addform.pics.split(index, 1)
    },
    async handleClick() {
      if (this.active === '1') {
        // 调取动态接口
        const res = await dongtai(this.checkkk)
        res.forEach((item) => {
          item.attr_vals =
            item.attr_vals.length > 0 ? item.attr_vals.split(',') : []
        })
        this.manydongtai = res
      } else if (this.active == '2') {
        // 调取静态参数
        this.onlyjingtai = await dongtai(this.checkkk, 'only')
      }
    },
    // 三级分类的事件
    handleChange(value) {},
    // 渲染下拉框数据列表
    async auto() {
      const res = await goodstype()
      console.log(res)
      this.options = res
    },
    // 判断是否选择了3级分类
    leave(activeName, oldActiveName) {
      if (oldActiveName == 0 && this.checkyyy.length !== 3) {
        this.$message({
          message: '请选择3级分类',
          type: 'error'
        })
        return false
      }
    }
  },
  computed: {
    checkkk() {
      return this.checkyyy[this.checkyyy.length - 1]
    }
  }
}
</script>

<style lang="less" scoped>
::v-deep .el-step__title {
  font-size: 12px !important;
}
</style>
